<?php

namespace App\Http\Controllers\Admin\Setting;

use App\Http\Controllers\Controller;
use App\Repositories\SystemConfigRepository;
use Illuminate\Http\Request;

class WebSiteController extends Controller
{

    public function __construct(Request $request, SystemConfigRepository $repository)
    {
        $this->request = $request;
        $this->repository = $repository;
    }

    public function index()
    {
        return $this->inertia('admin/setting/website/index', [
            'data' => $this->repository->getGroup('website'),
            'title' => '网站设置',
        ]);
    }

    public function update()
    {
        $params = $this->validate(
            $this->request,
            [
                'website_name' => 'required|string|max:50',
                'website_url' => 'required|url',
                'website_logo' => 'nullable|string',
                'website_title' => 'nullable|string|max:100',
                'website_keywords' => 'nullable|string|max:255',
                'website_description' => 'nullable|string|max:255',
                'website_icp' => 'nullable|string|max:50',
                'website_police_icp' => 'nullable|string|max:50',
                'website_copyright' => 'nullable|string|max:500',
                'website_analytics' => 'nullable|string|max:500',
                'website_contact' => 'nullable|string|max:500',
                'website_email' => 'nullable|email',
                'website_phone' => 'nullable|string',
                'website_address' => 'nullable|string',
                'website_wechat_qrcode' => 'nullable|string',
            ],
            [
                'website_name.*' => '网站名称不能为空',
                'website_url.*' => '网站地址不能为空',
                'website_logo.*' => '网站LOGO不能为空',
                'website_title.*' => '网站标题不能为空',
                'website_keywords.*' => '网站关键词不能为空',
                'website_description.*' => '网站描述不能为空',
                'website_icp.*' => '网站ICP备案号不能为空',
                'website_police_icp.*' => '网站公安备案号不能超过50个字符',
                'website_copyright.*' => '网站版权信息不能超过500个字符',
                'website_analytics.*' => '网站统计代码不能超过500个字符',
                'website_contact.*' => '联系方式不能超过500个字符',
                'website_email.*' => '邮箱格式错误',
                'website_phone.*' => '手机号格式错误',
                'website_address.*' => '地址格式错误',
                'website_wechat_qrcode' => '微信二维码格式错误',
            ]
        );

        $this->repository->updateGroup('website', $params);

        return redirect()->back()->with('message', '保存成功');
    }
}
